# R code file associated with LTAYLOR_Soil nematode functional diversity during vertebrate decomposition

library(tidyverse)
table2 <- read.csv(file="All_indices.csv")
str(table2)#checks to see classes of variables
table2 <- within(table2, {
  Day <-factor(Day)
  Cage.subject <- factor (Cage.subject)
  Type_treatment <- factor (Type_treatment)
}) #changes Day into a factor
table2a <- filter(table2,Location == "Cage")
table2b <- filter(table2,Location == "Cage control")
table2c <- filter(table2,Location == "Interface cage")
table2d <- filter(table2,Location == "Interface control")
sample_days <- as.factor(c(0,2,6,15,21,40,78,110,153,188,250,317))

#*************************************
# BOXPLOTS 
#*************************************

#ABUNDANCE CAGE CORES (boxplots) WITH CAGE CONTROL MEANS (added points)
pdf("Abuncore2.pdf", width = 4,height=3.2, useDingbats = FALSE)
#png("Abuncore1.png", width = 4,height=3.2, units = 'in',res = 1000)
boxplot(n~Day, data=table2a, xlab="Day", ylab="Abundance", ylim=c(0,40000),las=1, cex.axis=0.5,main="Abundance in Cores")
Acage_control_means <-as.vector(tapply(table2b$n, INDEX=table2b$Day, FUN=mean, simplify = TRUE), mode="numeric")
points (factor(sample_days), Acage_control_means,bg="red",col="black",cex=1, pch=21)
dev.off()

#ABUNDANCE INTERFACE (boxplots) WITH CAGE CONTROL MEANS (added points)
pdf("Abunint1.pdf", width = 4, height = 3.2, useDingbats = FALSE)
#png("Abunint1.png", width = 4,height=3.2, units = 'in',res = 1000)
boxplot(n~Day, data=table2c, xlab="Day", ylab="Abundance", ylim=c(0,40000),las=1,cex.axis=0.5, main="Abundance in Interfaces")
Aifc_control_means <-as.vector(tapply(table2d$n, INDEX=table2d$Day, FUN=mean, simplify = TRUE), mode="numeric")
#points (factor(sample_days), Aifc_control_means,bg="red",col="black", cex=1, pch=21)
#invisible <- as.factor(c(0,2,6,15,21))
#points (invisible, Aifc_control_means[1:5], bg="white", col="white", cex=1, pch=21)
points (sample_days [6:12], Aifc_control_means[6:12], bg="red", col="black", cex=1, pch=21)
dev.off()

#RICHNESS CAGE CORES (boxplot) WITH CAGE CONTROL MEANS (added points)
#png("Richcore.png", width = 6,height=4.8, units = 'in',res = 1000)
##above sizing uses cex=1.5 for dots and cex.axis=0.8
#png("Richcore.png", width = 4,height=3.2, units = 'in',res = 1000)
pdf("Richcore.pdf", width = 4,height=3.2, useDingbats = FALSE)
boxplot(Richness~Day, data=table2a, xlab="Day", ylab="Richness", ylim=c(0,30),las=1, cex.axis=0.5,main="Richness in Cores")
cage_control_means <-as.vector(tapply(table2b$Richness, INDEX=table2b$Day, FUN=mean, simplify = TRUE), mode="numeric")
points (factor(sample_days), cage_control_means,bg="red",cex=1, pch=21,col="black")
dev.off()

#RICHNESS INTERFACE CAGE (boxplot) WITH INTERFACE CONTROL MEANS (added points)
pdf("Richint1.pdf", width = 4, height = 3.2, useDingbats = FALSE)
#png("Richint1.png", width = 4,height=3.2, units = 'in',res = 1000)
boxplot(Richness~Day, data=table2c, xlab="Day", ylab="Richness", ylim=c(0,30),las=1, cex.axis=0.5, main="Richness in Interfaces")
ifc_control_means <-as.vector(tapply(table2d$Richness, INDEX=table2d$Day, FUN=mean, simplify = TRUE), mode="numeric")
#points (factor(sample_days), ifc_control_means,bg="red", cex=1, pch=21, col="black")
points (sample_days [6:12], ifc_control_means[6:12], bg="red", col="black", cex=1, pch=21)
dev.off()

#SHANNON CAGE CORES (boxplots) WITH CAGE CONTROL MEANS (added points)
pdf("Divcore.pdf", width = 4, height = 3.2, useDingbats = FALSE)
#png("Divcore.png", width = 4,height=3.2, units = 'in',res = 1000)
boxplot(Shannon~Day, data=table2a, xlab="Day", ylab="Diversity", ylim=c(0,3), las=1, cex.axis=0.5,main="Diversity in Cores")
Scage_control_means <-as.vector(tapply(table2b$Shannon, INDEX=table2b$Day, FUN=mean, simplify = TRUE), mode="numeric")
points (factor(sample_days), Scage_control_means,bg="red",col="black",cex=1, pch=21)
dev.off()

#SHANNON INTERFACE CAGE (boxplot) WITH INTERFACE CONTROL MEANS (added points) 
pdf("Divint.pdf", width = 4, height = 3.2, useDingbats = FALSE)
#png("Divint1.png", width = 4,height=3.2, units = 'in',res = 1000)
boxplot(Shannon~Day, data=table2c, xlab="Day", ylab="Diversity", ylim=c(0,3), las=1, cex.axis=0.5,main="Diversity in Interfaces")
Sifc_control_means <-as.vector(tapply(table2d$Shannon, INDEX=table2d$Day, FUN=mean, simplify = TRUE), mode="numeric")
#points (factor(sample_days), Sifc_control_means,bg="red",col="black", cex=1, pch=21)
points (sample_days [6:12], Sifc_control_means[6:12], bg="red", col="black", cex=1, pch=21)
dev.off()




#****************************************************************************
# ANOVAS (CORES) --  ABUNDANCE, RICHNESS, SHANNON
#**************************************************************************
library(car)
core_data <- rbind(table2a,table2b)

full_core_mod <- lm(n~Type_treatment+Day, data=core_data) #Abundance
full_core_mod <- lm(Richness~Type_treatment+Day, data=core_data) #Richness
full_core_mod <- lm(Shannon~Type_treatment+Day, data=core_data) #Shannon
Anova(full_core_mod) 
step(full_core_mod)#says to use all terms
TukeyHSD(aov(full_core_mod)) 

interaction_core_mod <- lm(n~Type_treatment*Day, data=core_data) #Abundance
interaction_core_mod <- lm(Richness~Type_treatment*Day, data=core_data) #Richness
interaction_core_mod <- lm(Shannon~Type_treatment*Day, data=core_data) #Shannon
Anova(interaction_core_mod)

#**************************************************************************
# Post-hoc t tests CORES
#*************************************************************************
T_day<- filter(core_data, Day==317) #adjust day
T_con <- filter(T_day, Type_treatment==1) #leave this alone
T_treat <-filter(T_day, Type_treatment==2) #leave this alone
t.test(T_con$n,T_treat$n, paired=TRUE) #ABUNDANCES
t.test(T_con$Richness,T_treat$Richness, paired=TRUE) #RICHNESS
t.test(T_con$Shannon,T_treat$Shannon, paired=TRUE) #SHANNON


#****************************************************************************
# ANOVAS (INTERFACES) -- simple model ABUNDANCE, RICHNESS, SHANNON
#**************************************************************************
int_data_prep <- rbind(table2c,table2d)
int_data <- filter (int_data_prep, Day != 0 & Day !=2 & Day != 6 & Day != 15 & Day != 21)

library(car)

full_int_mod <- lm(n~Type_treatment+Day, data=int_data) #Abundance
full_int_mod <- lm(Richness~Type_treatment+Day, data=int_data) #Richness
full_int_mod <- lm(Shannon~Type_treatment+Day, data=int_data) #Shannon
Anova(full_int_mod) 
step(full_int_mod)#says to use all terms
TukeyHSD(aov(full_int_mod)) 

interaction_int_mod <- lm(n~Type_treatment*Day, data=int_data) #Abundance
interaction_int_mod <- lm(Richness~Type_treatment*Day, data=int_data) #Richness
interaction_int_mod <- lm(Shannon~Type_treatment*Day, data=int_data) #Shannon
Anova(interaction_int_mod)
#**************************************************************************

#**************************************************************************
# Post-hoc t tests CORES
#*************************************************************************
T_day<- filter(int_data, Day==317) #adjust day
T_con <- filter(T_day, Type_treatment==1) #leave this alone
T_treat <-filter(T_day, Type_treatment==2) #leave this alone
t.test(T_con$n,T_treat$n, paired=TRUE) #ABUNDANCES
t.test(T_con$Richness,T_treat$Richness, paired=TRUE) #RICHNESS
t.test(T_con$Shannon,T_treat$Shannon, paired=TRUE) #SHANNON


#************************************************************************
# RDA 
#***********************************************************************
# This is the core plot
library(vegan)
###

Ord_core <- read.csv(file="ordination_cage.csv")
#null.test<-rda(Ord_core[,8:86]~1,scale=TRUE) #null model built to say species are not responding to anything

#pdf("RDAcore_spider1.pdf", width = 6, height = 6, useDingbats = FALSE)
pdf("RDAcore.pdf", width = 6, height = 6)
#png("RDAcoretest.png", width = 6,height=6, units = 'in',res = 300)
#core.pca <- rda(Ord_core[,8:86]~Ord_core$Controls_and_stages, scale=TRUE) #Full model saying X is explained by Y (location)
core.pca <- rda(Ord_core[,8:86]~Ord_core$Controls_and_stages, scale=TRUE) #Full model saying X is explained by Y (location) 
plot(core.pca, display="lc", ylim = c(-2,3), main="RDA biplot Cores")

#ordiellipse(core.pca, Ord_core$Controls_and_stages, col=c("orange","yellow","orange4", "black","red","red4","green","blue4","grey80","grey50","blue","green4","magenta"), draw="polygon")#,label = TRUE, cex=0.5) #all sampling dates individually
ordiellipse(core.pca, Ord_core$Controls_and_stages, col=c("black","red2","orange2","yellow","green2","blue2","magenta"), draw="polygon")#,label = TRUE, cex=0.5) #colored by morphological decomp stage
#ordispider(core.pca, Ord_core$Controls_and_stages, col=c("black","red2","orange2","yellow","green2","blue2","magenta"), label = FALSE, cex=0.5) #labels are turned off just now
#add legend
dev.off()

#legend built as a separate entityput together in Illustrator when finished
#png("RDAcore_legend.png", width = 3.25,height=4, units = 'in',res = 1000)
pdf("RDAcore_legend.pdf", width = 3.25, height = 4, useDingbats = FALSE)
plot(-0.5:12.5, -0.5:12.5, type="n", yaxt= "n",  ylab="", xlab = "", main="Legend")
points (1, 11, pch=21, cex=1.6, col="black", bg="red2", lwd=1)
text(2,10.7, "Fresh", cex=0.9,adj = c(0,0))
points (1, 10, pch=21, cex=1.6, col="black", bg="orange1", lwd=1)
text(2,9.7, "Bloat", cex=0.9,adj = c(0,0))
points (1, 9, pch=21, cex=1.6, col="black", bg="yellow", lwd=1)
text(2,8.7, "Active", cex=0.9,adj = c(0,0))
points (1, 8, pch=21, cex=1.6, col="black", bg="green2", lwd=1)
text(2,7.7, "Advanced", cex=0.9,adj = c(0,0))
points (1, 7, pch=21, cex=1.6, col="black", bg="blue2", lwd=1)
text(2,6.7, "Early Skeletonization", cex=0.9,adj = c(0,0))
points (1, 6, pch=21, cex=1.6, col="black", bg="magenta", lwd=1)
text(2,5.7, "Late Skeletonization", cex=0.9,adj = c(0,0))
points (1, 5, pch=21, cex=1.6, col="black", bg="grey", lwd=1)
text(2,4.7, "Control", cex=0.9,adj = c(0,0))
dev.off()

# stop here to get nice tidy graph without the spiders.
anova(core.pca)
summary (core.pca)

# to add spiders (note color call is old textwould need to match to the above)
ordispider(core.pca, Ord_core$Controls_and_stages, col=1:12, label = FALSE, cex=0.5) #Put our datapoints on the map and adds labels--which are turned off just now

# This is the cage interface plot
library(vegan)
###
Ord_int <- read.csv(file="ordination_int.csv")
#null.test<-rda(Ord_int[,8:85]~1,scale=TRUE) #null model built to say species are not responding to anything

pdf("RDAint2.pdf", width = 6,height = 6,useDingbats = FALSE)
#png("RDAint.png", width = 6,height=6, units = 'in',res = 300)
int.pca1 <- rda(Ord_int[,8:85]~Ord_int$Controls_and_stages, scale=TRUE) #Full model saying X is explained by Y (location) 
plot(int.pca1, display="lc", ylim=c(-3,2), main="RDA biplot Interfaces")
ordiellipse(int.pca1, Ord_int$Controls_and_stages, col=c("black","yellow", "green2","blue2","magenta"), draw="polygon")#,label = TRUE, cex=0.5)
# stop here to get nice tidy graph without the spiders.

#ordispider(int.pca1, Ord_int$Controls_and_stages, col=c("black","yellow", "green2","blue2","magenta"), label = FALSE, cex=0.5) #Put our datapoints on the map and adds labels--which are turned off just now
dev.off()


png("RDAint_legend.png", width = 3.25,height=4, units = 'in',res = 1000)
plot(-0.5:12.5, -0.5:12.5, type="n", yaxt= "n",  ylab="", xlab = "", main="Legend")
points (1, 12, pch=21, cex=1.6, col="black", bg="grey50", lwd=1)
text(2,11.7, "Control", cex=0.9, adj = c(0,0))
points (1, 11, pch=21, cex=1.6, col="black", bg="yellow", lwd=1)
text(2,10.7, "Active", cex=0.9,adj = c(0,0))
points (1, 10, pch=21, cex=1.6, col="black", bg="green2", lwd=1)
text(2,9.7, "Advanced", cex=0.9,adj = c(0,0))
points (1, 9, pch=21, cex=1.6, col="black", bg="blue2", lwd=1)
text(2,8.7, "Early Skeletonization", cex=0.9,adj = c(0,0))
points (1, 8, pch=21, cex=1.6, col="black", bg="magenta", lwd=1)
text(2,7.7, "Late Skeletonization", cex=0.9,adj = c(0,0))
abline(v=0)
abline(h=0)
dev.off()

#This is the combined graph: cores and interfaces

Ord_comb <- read.csv(file="Ordination_combined.csv")
pdf("RDAcomb1.pdf", width = 6,height = 6,useDingbats = FALSE)
#png("RDAcomb1.png", width = 6,height = 6, units='in',res=300)
comb.rda1 <- rda(Ord_comb[,9:98]~Ord_comb$Controls_and_stages, scale=TRUE) #Full model saying X is explained by Y (location) 
plot(comb.rda1,display = "lc",ylim=c(-4,4), main="RDA biplot core vs int")
ordiellipse(comb.rda1, Ord_comb$Depth, col=c("blue2","red2"), draw="polygon")#,label = TRUE, cex=0.5)
# stop here to get nice tidy graph without the spiders.
#ordispider(comb.rda1, Ord_comb$Depth, col=c("blue2","red2"), label = FALSE, cex=0.5)
#Put our datapoints on the map and adds labels--which are turned off just now
stage.names <- levels(Ord_comb$Depth)
legend("left",col = c(1:12), lty =1, legend = stage.names, cex=0.5)

dev.off()

#***********************************************************************
# HEATMAPS
#***********************************************************************
library(RColorBrewer)
# want genera as rows and the first row NOT to show up as a named column since this dataframe has to used as a matrix
heat_core <- read.csv(file="Rheat_core.csv", row.names = 1)#, header=TRUE)
heat_core <-subset(heat_core, select= -c(Trophic)) #removes the trophic row
heat_core_m <-as.matrix(heat_core) #sets the data as a matrix

heat_int <- read.csv(file="Rheat_int.csv", row.names = 1)#, header=TRUE)
heat_int <-subset(heat_int, select= -c(Trophic)) #removes the trophic row
heat_int_m <-as.matrix(heat_int) #sets the data as a matrix


install.packages("pheatmap")
library(pheatmap)
# DO NOT attempt to use pdf language to save the plot--the program crashes. Instead export it to a 4x7 in portrait
pheatmap(heat_core_m,cluster_rows = FALSE, cluster_cols = FALSE, fontsize = 7,angle_col = c("45"))

pheatmap(heat_int_m,cluster_rows = FALSE, cluster_cols = FALSE, fontsize = 7,angle_col = c("45"))
#***********************************************************************

#*************************************
# Temperature and conductivity data
#*************************************

# Temperature Means plot
pdf("Temperature.pdf", width = 6, height = 4.5)
#png("temp.png", width = 6,height=4.8, units = 'in',res = 300)
plot(Decagon$Study.day, Decagon$Ambient, xlim=c(0,30),ylim=c(0,35),type="l", lty=1,lwd=1, ylab="Degrees C ", xlab = "Day", col="gray")
lines(Decagon$Study.day, Decagon$Soiltemp_conmeans, col="black", lty=1, lwd=1)
lines(Decagon$Study.day, Decagon$Soiltemp_means, col="blue3", lty=1, lwd=3)
lines(Decagon$Study.day, Decagon$internal_means, col="red3", lty=1, lwd=3)
legend("bottomright", legend=c("Internal","Soil","Soil control","Ambient air"),lty=c(1,1,1,1),col=c("red3","blue3","black","gray"), lwd=c(3,3,1,1),cex=c(1,1,1,1),bty="n")
dev.off()

# Moisture Means plot
pdf("Moisture.pdf", width = 6, height = 4.5)
#png("moisture.png", width = 6,height=4.8, units = 'in',res = 300)
plot(Decagon$Study.day, Decagon$Moisture_conmeans, xlim=c(0,100),ylim=c(30,50),type="l", lty=1,lwd=1, ylab="%Volumetric Water Content ", xlab = "Day", col="black")
lines(Decagon$Study.day, Decagon$Moisture_means, col="blue3", lty=1, lwd=3)
legend("topright", legend=c("Soil","Soil control"),lty=c(1,1),col=c("blue3","black"), lwd=c(3,1),cex=c(1,1),bty="n")
dev.off()

# EC Means plot
pdf("ED10.pdf", width = 6, height = 4.5)
#png("EC.png", width = 6,height=4.8, units = 'in',res = 300)
plot(Decagon$Study.day, Decagon$EC_conmeans, xlim=c(0,100),ylim=c(0,2),type="l", lty=1,lwd=1, ylab="mS cm-1 ", xlab = "Day", col="black")
lines(Decagon$Study.day, Decagon$EC_means, col="blue3", lty=1, lwd=3)
legend("topright", legend=c("Soil","Soil control"),lty=c(1,1),col=c("blue3","black"), lwd=c(3,1),cex=c(1,1),bty="n")
dev.off()




#***************************************************************************
# FAUNAL PROFILE - cores
#***************************************************************************
library(tidyverse)
# prepare and sort the dataframes
table2 <- read.csv(file="All_indices.csv")
str(table2)#checks to see classes of variables
table2a <- filter(table2,Location == "Cage")
table2b <- filter(table2,Location == "Cage control")
table2c <- filter(table2,Location == "Interface cage")
table2d <- filter(table2,Location == "Interface control")

# controls, plotted in small grey points and add lines
pdf("Faunalcore_final2.pdf", width = 6, height = 4.8, useDingbats = FALSE)
#png("Faunalcore_final.png", width = 6,height=4.8, units = 'in',res = 600)
plot(table2b$SI, table2b$EI, xlim=c(0,100),ylim=c(0,100),las=1,ylab="Enrichment Index (EI)", xlab = "Structure Index (SI)", main= "Faunal Profile - cores",pch=21, cex=1, col="black", bg="gray")#pch=19, col="gray", cex=1)
abline(v=50, col="gray", lty=2)
abline (h=50, col="gray", lty=2)

# initial, plotted in red2
init <- filter(table2a,Stage == "Placement")
points (init$SI, init$EI, pch=21, cex=1.75, col="black", bg="red2", lwd=1)
# fresh, plotted in red
fresh <- filter(table2a,Stage == "Fresh")
points (fresh$SI, fresh$EI, pch=21, cex=1.75, col="black", bg="red", lwd=1)
# bloat, plotted in orange
bloat <- filter(table2a,Stage == "Bloat")
points (bloat$SI, bloat$EI, pch=21, cex=1.75, col="black", bg="orange", lwd=1)
# active, plotted in yellow
active <- filter(table2a,Stage == "Active")
points (active$SI, active$EI, pch=21, cex=1.75, col="black", bg="yellow", lwd=1)
# advanced 1, plotted in springgreen
advanced <- filter(table2a,Stage == "Advanced")
points (advanced$SI, advanced$EI, pch=21, cex=1.75, col="black", bg="springgreen", lwd=1)
#advanced 2, plotted in green3 
late <- filter(table2a,Stage == "Late")
points (late$SI, late$EI, pch=21, cex=1.75, col="black", bg="green3", lwd=1)
# early skeletonization 1 (skel_June), plotted in light blue
SJune <- filter(table2a,Stage == "Skeletonization")
points (SJune$SI, SJune$EI, pch=21, cex=1.75, col="black", bg="light blue", lwd=1)
# early skeletonization 2 (skel_July), plotted in cornflower blue
SJuly <- filter(table2a,Stage == "Skel_July")
points (SJuly$SI, SJuly$EI, pch=21, cex=1.75, col="black", bg="cornflower blue", lwd=1)
# late skeletonization 1 (skel_Aug), plotted in blue
SAug <- filter(table2a,Stage == "Skel_Aug")
points (SAug$SI, SAug$EI, pch=21, cex=1.75, col="black", bg="blue", lwd=1)
# late skeletonization 2 (skel_Oct) plotted in purple2
SOct <- filter(table2a,Stage == "Skel_Oct")
points (SOct$SI, SOct$EI, pch=21, cex=1.75, col="black", bg="purple2", lwd=1)
# skeletonization 3 (skel_Dec), plotted in  purple 3
SDec <- filter(table2a,Stage == "Skel_Dec")
points (SDec$SI, SDec$EI, pch=21, cex=1.75, col="black", bg="purple3", lwd=1)
# skeletonization 4 (skel_Feb), plotted in purple4
SFeb <- filter(table2a,Stage == "Skel_Feb")
points (SFeb$SI, SFeb$EI, pch=21, cex=1.75, col="black", bg="purple4", lwd=1)
dev.off()


#INTERFACE FAUNAL PLOT
#png("Faunalint_final.png", width = 6,height=4.8, units = 'in',res = 600)
pdf("Faunalint_final2.pdf", width = 6, height = 4.8, useDingbats = FALSE)
table2e <- filter(table2d, n > 0)
plot(table2e$SI, table2e$EI, xlim=c(0,100),ylim=c(0,100),las=1,ylab="Enrichment Index (EI)", xlab = "Structure Index (SI)", main= "Faunal Profile - Interfaces",pch=21, cex=1, col="black", bg="gray")#pch=19, col="gray", cex=1)
abline(v=50, col="gray", lty=2)
abline (h=50, col="gray", lty=2)

# advanced 2 (late), plotted in green3
lateI <- filter(table2c,Stage == "Late")
points (lateI$SI, lateI$EI, pch=21, cex=1.75, col="black", bg="green3", lwd=1)
# early skeletonization 1 (skel_June), plotted in light blue
SJuneI <- filter(table2c,Stage == "Skeletonization")
points (SJuneI$SI, SJuneI$EI, pch=21, cex=1.75, col="black", bg="light blue", lwd=1)
# early skeletonization 2 (skel_July), plotted in cornflower blue
SJulyI <- filter(table2c,Stage == "Skel_July")
points (SJulyI$SI, SJulyI$EI, pch=21, cex=1.75, col="black", bg="cornflower blue", lwd=1)
# late skeletonization 1 (skel_Aug), plotted in blue
SAugI <- filter(table2c,Stage == "Skel_Aug")
points (SAugI$SI, SAugI$EI, pch=21, cex=1.75, col="black", bg="blue", lwd=1)
# late skeletonization 2 (skel_Oct), plotted in purple2 
SOctI <- filter(table2c,Stage == "Skel_Oct")
points (SOctI$SI, SOctI$EI, pch=21, cex=1.75, col="black", bg="purple2", lwd=1)
# late skeletonization 3 (skel_Dec), plotted in purple3 
SDecI <- filter(table2c,Stage == "Skel_Dec")
points (SDecI$SI, SDecI$EI, pch=21, cex=1.75, col="black", bg="purple3", lwd=1)
# late skeletonization 4 (skel_Feb), plotted in purple4
SFebI <- filter(table2c,Stage == "Skel_Feb")
points (SFebI$SI, SFebI$EI, pch=21, cex=1.75, col="black", bg="purple4", lwd=1)
dev.off()

pdf("Faunallegen_final2.pdf", width=3.5, height=4, useDingbats = FALSE)
#png("Faunallegend_final.png", width = 3.25,height=4, units = 'in',res = 500)
plot(-0.5:12.5, -0.5:12.5, type="n", yaxt= "n", bty="n", xlab="", ylab="")
points (1, 12, pch=21, cex=1.6, col="black", bg="red2", lwd=1)
text(2,11.7, "Placement", cex=0.9, adj = c(0,0))
points (1, 11, pch=21, cex=1.6, col="black", bg="red", lwd=1)
text(2,10.7, "Fresh", cex=0.9,adj = c(0,0))
points (1, 10, pch=21, cex=1.6, col="black", bg="orange", lwd=1)
text(2,9.7, "Bloat", cex=0.9,adj = c(0,0))
points (1, 9, pch=21, cex=1.6, col="black", bg="yellow", lwd=1)
text(2,8.7, "Active", cex=0.9,adj = c(0,0))
points (1, 8, pch=21, cex=1.6, col="black", bg="spring green", lwd=1)
text(2,7.7, "Advanced 1", cex=0.9,adj = c(0,0))
points (1, 7, pch=21, cex=1.6, col="black", bg="green3", lwd=1)
text(2,6.7, "Advanced 2", cex=0.9,adj = c(0,0))
points (1, 6, pch=21, cex=1.6, col="black", bg="light blue", lwd=1)
text(2,5.7, "Early Skeletonization 1", cex=0.9,adj = c(0,0))
points (1, 5, pch=21, cex=1.6, col="black", bg="cornflower blue", lwd=1)
text(2,4.7, "Early Skeletonization 2", cex=0.9,adj = c(0,0))
points (1, 4, pch=21, cex=1.6, col="black", bg="blue", lwd=1)
text(2,3.7, "Late Skeletonization 1", cex=0.9,adj = c(0,0))
points (1, 3, pch=21, cex=1.6, col="black", bg="purple2", lwd=1)
text(2,2.7, "Late Skeletonization 2", cex=0.9,adj = c(0,0))
points (1, 2, pch=21, cex=1.6, col="black", bg="purple3", lwd=1)
text(2,1.7, "Late Skeletonization 3", cex=0.9,adj = c(0,0))
points (1, 1, pch=21, cex=1.6, col="black", bg="purple4", lwd=1)
text(2,0.7,"Late Skeletonization 4", cex=0.9,adj = c(0,0))
points (1, 0, pch=21, cex=1.6, col="black", bg="gray", lwd=1)
text(2,-0.3,"Controls", cex=0.9,adj = c(0,0))
dev.off()

#****************************************************************************
# STACKED BAR GRAPHS
#****************************************************************************

#the following code produces the relative feeding guild stacked bar graph by day
library(tidyverse)
library(ggplot2)
library(RColorBrewer)

stackedbartable <- read.csv(file="stackedbar_trophiccore.csv")
stackedbartable <- read.csv(file="stackedbar_trophicint.csv")
str(stackedbartable)

# by trophic composition
pdf("stackedbar_trophic_int2.pdf", width = 6, height = 4)
#pdf("stackedbar_trophic_core2.pdf", width = 6, height = 4)
ggplot(stackedbartable, aes(x=Day, y=Percent, fill=GUILD))+
  geom_bar(stat="identity")+
  scale_fill_brewer( type = "div" , palette = "Set1" )+
  labs(x="Day")+
  ylab("Relative Abundance")+
  guides(fill=guide_legend(reverse=FALSE, keywidth = 1, keyheight = 1))+
  theme_classic()+
  theme(axis.text.x=element_text(size=12, angle=90), axis.text.y=element_text(size=12), axis.title=element_text(size=14),
        legend.title=element_blank(), legend.text=element_text(size=12))
dev.off()

# by cp class composition
stackedbartable <- read.csv(file="stackedbar_cpcore.csv")
stackedbartable <- read.csv(file="stackedbar_cpint.csv")
str(stackedbartable)
stackedbartable$cp_class <- as.factor(stackedbartable$cp_class)

pdf("stackedbar_cp_int.pdf", width = 6, height = 4)
#pdf("stackedbar_cp_core.pdf", width = 6, height = 4, useDingbats = FALSE)
ggplot(stackedbartable, aes(x=Day, y=percent, fill=cp_class))+
  geom_bar(stat="identity")+
  scale_fill_brewer( type = "div" , palette = "Set1" )+
  labs(x="Day")+
  ylab("Relative Abundance")+
  guides(fill=guide_legend(reverse=FALSE, keywidth = 1, keyheight = 1))+
  theme_classic()+
  theme(axis.text.x=element_text(size=12, angle=90), axis.text.y=element_text(size=12), axis.title=element_text(size=14),
        legend.title=element_blank(), legend.text=element_text(size=12))
dev.off()

#*******************************************************************************
